Timestamp Debouncing

date: 2025-05-18
author: Peterino

Seeing this problem appear a few times. Say you have a system that that receives a lot of messages. You'd run some operation in response to the message.

Very functional very demure.

However what if the message is some kind of request or update? And you may receive a lot of messages all at the same time. Especially bad if something like this. In particular I have a debugging system which shows an onscreen display of some variables and reacts in a non-time critical way to changes in some slow values.

Assuming that Update debugging info is very expensive this could be bad.

Ideally I would like to have a debouncing for all these edits to happen at the same time- that is apply the expensive operation only after some longer period of time after the debugging information has been completed.

A really elegant and tiny way to do this is record two 64 bit timestamps

  1. Last Edit time
  2. Last Update time

Then the logic for correct updates becomes quite easy

if (lastEdit > lastUpdate && getTimeStamp() > lastEdit + debounceDelay)
{
    lastUpdate = lastUpdate
}

This was such a common pattern and I kept writing oddly complex logic to deal with this. and I think every time I've ended up rewriting this.

So- just putting this entry into my notebook for future reference.